11088 - End up with more teams (DP), 11282 - Mixing invitations & 1481 - Arrange...
[and.git] / Mi manual de algoritmos / version_maraton_nacional_2008 / src / java / maps_sets.tex
blob6b77ee3d264ef138a818360eb9fe1f227827d6ff
1 % Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
3 {\ttfamily \raggedright {
4 \noindent
5 \mbox{}\textbf{\textcolor{RoyalBlue}{import}}\ java\textcolor{BrickRed}{.}util\textcolor{BrickRed}{.*;} \\
6 \mbox{} \\
7 \mbox{}\textbf{\textcolor{Blue}{public}}\ \textbf{\textcolor{Blue}{class}}\ Ejemplo\ \textcolor{Red}{\{} \\
8 \mbox{}\ \ \ \ \textbf{\textcolor{Blue}{public}}\ \textbf{\textcolor{Blue}{static}}\ \textcolor{ForestGreen}{void}\ \textbf{\textcolor{Black}{main}}\textcolor{BrickRed}{(}String\textcolor{BrickRed}{[]}\ args\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
9 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{/*}} \\
10 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ Mapas}} \\
11 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ Tanto\ el\ HashMap\ como\ el\ TreeMap\ funcionan,\ pero\ tienen\ diferentes\ detalles}} \\
12 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ y\ difieren\ en\ algunos\ métodos\ (Ver\ API).}} \\
13 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ */}} \\
14 \mbox{}\ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}Maps"{}}}\textcolor{BrickRed}{);} \\
15 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{//TreeMap$<$String,\ Integer$>$\ m\ =\ new\ TreeMap$<$String,\ Integer$>$();}} \\
16 \mbox{}\ \ \ \ \ \ \ \ HashMap\textcolor{BrickRed}{$<$}String\textcolor{BrickRed}{,}\ Integer\textcolor{BrickRed}{$>$}\ m\ \textcolor{BrickRed}{=}\ \textbf{\textcolor{Blue}{new}}\ HashMap\textcolor{BrickRed}{$<$}String\textcolor{BrickRed}{,}\ Integer\textcolor{BrickRed}{$>$();} \\
17 \mbox{}\ \ \ \ \ \ \ \ m\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{put}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}Hola"{}}}\textcolor{BrickRed}{,}\ \textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Integer}}\textcolor{BrickRed}{(}\textcolor{Purple}{465}\textcolor{BrickRed}{));} \\
18 \mbox{}\ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}m.size()\ =\ "{}}}\ \textcolor{BrickRed}{+}\ m\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{size}}\textcolor{BrickRed}{());} \\
19 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
20 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{if}}\ \textcolor{BrickRed}{(}m\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{containsKey}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}Hola"{}}}\textcolor{BrickRed}{))}\textcolor{Red}{\{} \\
21 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}m\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{get}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}Hola"{}}}\textcolor{BrickRed}{));} \\
22 \mbox{}\ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
23 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
24 \mbox{}\ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}m\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{get}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}Objeto\ inexistente"{}}}\textcolor{BrickRed}{));} \\
25 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
26 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
27 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{/*}} \\
28 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ Sets}} \\
29 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ La\ misma\ diferencia\ entre\ TreeSet\ y\ HashSet.}} \\
30 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ */}} \\
31 \mbox{}\ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}}}\texttt{\textcolor{CarnationPink}{\textbackslash{}n}}\texttt{\textcolor{Red}{Sets"{}}}\textcolor{BrickRed}{);} \\
32 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{/*}} \\
33 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ *\ *OJO:\ El\ HashSet\ no\ está\ en\ orden,\ el\ TreeSet\ sí.}} \\
34 \mbox{}\textit{\textcolor{Brown}{\ \ \ \ \ \ \ \ \ */}} \\
35 \mbox{}\ \ \ \ \ \ \ \ \textit{\textcolor{Brown}{//HashSet$<$Integer$>$\ s\ =\ new\ HashSet$<$Integer$>$();}} \\
36 \mbox{}\ \ \ \ \ \ \ \ TreeSet\textcolor{BrickRed}{$<$}Integer\textcolor{BrickRed}{$>$}\ s\ \textcolor{BrickRed}{=}\ \textbf{\textcolor{Blue}{new}}\ TreeSet\textcolor{BrickRed}{$<$}Integer\textcolor{BrickRed}{$>$();} \\
37 \mbox{}\ \ \ \ \ \ \ \ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textcolor{Purple}{3576}\textcolor{BrickRed}{);}\ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Integer}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}54"{}}}\textcolor{BrickRed}{));}\ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{add}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{new}}\ \textbf{\textcolor{Black}{Integer}}\textcolor{BrickRed}{(}\textcolor{Purple}{1000000007}\textcolor{BrickRed}{));} \\
38 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
39 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{if}}\ \textcolor{BrickRed}{(}s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{contains}}\textcolor{BrickRed}{(}\textcolor{Purple}{54}\textcolor{BrickRed}{))}\textcolor{Red}{\{} \\
40 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}54\ presente."{}}}\textcolor{BrickRed}{);} \\
41 \mbox{}\ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
42 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\
43 \mbox{}\ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{if}}\ \textcolor{BrickRed}{(}s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{isEmpty}}\textcolor{BrickRed}{()}\ \textcolor{BrickRed}{==}\ \textbf{\textcolor{Blue}{false}}\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
44 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}s.size()\ =\ "{}}}\ \textcolor{BrickRed}{+}\ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{size}}\textcolor{BrickRed}{());} \\
45 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ Iterator\textcolor{BrickRed}{$<$}Integer\textcolor{BrickRed}{$>$}\ i\ \textcolor{BrickRed}{=}\ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{iterator}}\textcolor{BrickRed}{();} \\
46 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \textbf{\textcolor{Blue}{while}}\ \textcolor{BrickRed}{(}i\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{hasNext}}\textcolor{BrickRed}{())}\textcolor{Red}{\{} \\
47 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}i\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{next}}\textcolor{BrickRed}{());} \\
48 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{remove}}\textcolor{BrickRed}{();} \\
49 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
50 \mbox{}\ \ \ \ \ \ \ \ \ \ \ \ System\textcolor{BrickRed}{.}out\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{println}}\textcolor{BrickRed}{(}\texttt{\textcolor{Red}{"{}s.size()\ =\ "{}}}\ \textcolor{BrickRed}{+}\ s\textcolor{BrickRed}{.}\textbf{\textcolor{Black}{size}}\textcolor{BrickRed}{());} \\
51 \mbox{}\ \ \ \ \ \ \ \ \textcolor{Red}{\}} \\
52 \mbox{}\ \ \ \ \textcolor{Red}{\}} \\
53 \mbox{}\textcolor{Red}{\}} \\
54 \mbox{} \\
56 } \normalfont\normalsize